//
//  MainTabbar.swift
//  WeiBoText
//
//  Created by 刘世瑛 on 16/1/10.
//  Copyright © 2016年 刘世瑛. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {
   //记录中间按钮
    var CenterBtn:UIButton?
    
    //重写父类方法
    override init(frame: CGRect) {
       
        super.init(frame: frame)
        
        addLogoinButton()
    }
    
   //通过其他方式崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    
    
    //布局调用
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = bounds.width / 5
        let h = bounds.height
        let  rect = CGRect(x: 0, y: 0, width: w, height: h)
        var index :CGFloat = 0
        for view in subviews
        {
            
            
          if view.isKindOfClass(NSClassFromString("UITabBarButton")!)
          {
           
             view.frame = CGRectOffset(rect, index * w, 0)
             index +=  index == 1 ? 2 : 1
        
            
            }
            CenterBtn!.frame = CGRectOffset(rect , w * 2 , 0)
            
        }
        
        
        
        
    }
    
    
   func addLogoinButton(){
    
    
    let btn = UIButton()
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
    
    btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
    btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)

    CenterBtn = btn
    addSubview(btn)
    
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
